package zhoug.zui.list;

import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import zhoug.common.anim.ZAnimations;
import zhoug.logging.Logger;
import zhoug.zui.R;
import zhoug.zui.ZUIEmptyView;
import zhoug.zui.dialog.ZUIBaseDialog;

/**
 * 选择列表支持单选和多选
 *
 * @Author: zhoug
 * @Date: 2024-07-08
 * @Description:
 */
public abstract class ZUISelectionListDialog<T> extends ZUIBaseDialog {
    private static final String TAG = ">>>ZUISelectionListDialog";
    protected RecyclerView mSelectionList;
    protected ZUIEmptyView mEmptyView;
    protected SimpleSelectListAdapter<T> mListAdapter;
    protected List<T> data = new ArrayList<>();
    protected String reLoadButtonText="重新加载";//emptyView中的按钮文本
    protected zhoug.zui.ints.OnClickListener reLoadOnClickListener;//emptyView中的按钮监听
    protected String emptyHint="没有数据";
    public ZUISelectionListDialog(@NonNull Context context) {
        super(context);
        setFullWidth(true);
        setAttributes(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM);
        setCancelable(true);
        setCanceledOnTouchOutside(true);
        setWindowAnimations(ZAnimations.ANIM_DOWN);
        mListAdapter = new SimpleSelectListAdapter<>() {
            @Override
            public String getItemName(T itemData) {
                return ZUISelectionListDialog.this.getItemName(itemData);
            }
        };
        mListAdapter.setData(data);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.zui_selection_list_dialog;
    }

    @Override
    public void show() {
        super.show();
        notifyDataChange();
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        Logger.d(TAG, this.getClass().getCanonicalName()+":init");
        mSelectionList = findViewById(R.id.selection_list);
        mEmptyView = findViewById(R.id.selection_emptyView);
        mSelectionList.setAdapter(mListAdapter);
        mSelectionList.setLayoutManager(new LinearLayoutManager(getContext()));
        int height = getHeight();
        int maxHeight = getMaxHeight();
        int emptyViewHeight = 0;
        if (height > 0) {
            emptyViewHeight = height;
        } else if (maxHeight > 0) {
            emptyViewHeight = maxHeight;
        }
        if (emptyViewHeight > 0) {
            ViewGroup.LayoutParams layoutParams = mEmptyView.getLayoutParams();
            layoutParams.height = emptyViewHeight;
            mEmptyView.setLayoutParams(layoutParams);
        }
    }


    /**
     * 是否正在加载数据
     *
     * @param loading
     */
    public void showLoading(boolean loading) {
        if(mSelectionList==null)return;
        if (loading) {
            mSelectionList.setVisibility(View.GONE);
            mEmptyView.setVisibility(View.VISIBLE);
            mEmptyView.show(true);
        } else {
            mSelectionList.setVisibility(View.VISIBLE);
            if (!mEmptyView.isHide()) {
                mEmptyView.hide();
            }
        }
    }

    /**
     * 数据加载成功
     *
     * @param data
     */
    public void loadSuccess(List<T> data) {
        setData(data);
        showLoading(false);
        if (isShowing()) {
            notifyDataChange();
        }
    }

    /**
     * 数据加载失败
     *
     * @param error
     */
    public void loadError(String error) {
        if(mSelectionList==null)return;
        mSelectionList.setVisibility(View.GONE);
        mEmptyView.setVisibility(View.VISIBLE);
        if (reLoadOnClickListener != null) {
            mEmptyView.show(error, null, reLoadButtonText, reLoadOnClickListener);
        } else {
            mEmptyView.show(error, null);
        }
    }


    /**
     * 设置EmptyView中的按钮 数据加载重新加载按钮监听
     *
     * @param reLoadButtonText
     * @param reLoadOnClickListener
     */
    public void setReLoadButton(String reLoadButtonText, zhoug.zui.ints.OnClickListener reLoadOnClickListener) {
        this.reLoadButtonText = reLoadButtonText;
        this.reLoadOnClickListener = reLoadOnClickListener;
    }

    /**
     * 数据加载重新加载按钮监听,为null时隐藏按钮
     * @param reLoadOnClickListener 重新加载回调
     */
    public void setReLoadButton( zhoug.zui.ints.OnClickListener reLoadOnClickListener) {
        this.reLoadOnClickListener = reLoadOnClickListener;
    }

    /**
     * 没有数据提示文本
     * @param emptyHint 提示文本 默认"没有数据"
     */
    public void setEmptyHint(String emptyHint) {
        this.emptyHint = emptyHint;
    }


    public abstract String getItemName(T itemData);

    /**
     * 设置对齐方式
     * @param textAlign {@link Gravity#CENTER}
     */
    public void setTextAlign( int textAlign) {
        mListAdapter.setTextAlign(textAlign);
    }


    /**
     * 设置列表数据
     * @param data 列表数据
     */
    public void setData(List<T> data) {
        this.data.clear();
        if (data != null && !data.isEmpty()) {
            this.data.addAll(data);
        }
    }

    /**
     * 获取列表数据
     * @return 列表数据
     */
    public List<T> getData() {
        return this.data;
    }

    /**
     * 获取指定位置的数据
     * @param position 位置 0-data.size-1
     * @return 数据
     */
    public T getItemData(int position) {
        return mListAdapter.getItemData(position);
    }

    /**
     * 设置选择监听器
     * @param onSelectChangeListener 选择监听器
     */
    public void setOnSelectChangeListener(OnSelectChangeListener<T> onSelectChangeListener) {
        mListAdapter.setOnSelectChangeListener(new OnSelectChangeListener<T>() {
            @Override
            public void onSelectChange(@NonNull List<T> select) {
                if (!mListAdapter.isMultiSelect()) {
                    dismiss();
                }
                if (onSelectChangeListener != null) {
                    onSelectChangeListener.onSelectChange(select);
                }
            }

            @Override
            public void onClearSelect() {
                if (!mListAdapter.isMultiSelect()) {
                    dismiss();
                }
                if (onSelectChangeListener != null) {
                    onSelectChangeListener.onClearSelect();
                }
            }
        });
    }

    /**
     * 设置多选
     * @param multi true:多选,false:单选
     */
    public void setMultiSelect(boolean multi) {
        mListAdapter.setMultiSelect(multi);
    }

    /**
     * 设置选择的数据
     * @param select 数据
     */
    public void setSelect(T select) {
        mListAdapter.setSelect(select);
    }

    /**
     * 取消选择
     * @param data
     */
    public void clearSelect(T data){
        mListAdapter.clearSelect(data);
    }

    /**
     * 设置选择的数据
     * @param select 数据集合
     */
    public void setSelect(List<T> select) {
        mListAdapter.setSelect(select);
    }

    /**
     * 设置是否显示勾选图标
     * @param show true:显示
     */
    public void showSelectImage(boolean show) {
        mListAdapter.showSelectImage(show);
    }

    /**
     * 清空选择
     */
    public void clearSelect() {
        mListAdapter.clearSelect();
    }

    /**
     * 是否是多选
     */
    public boolean isMultiSelect() {
      return  mListAdapter.isMultiSelect();
    }

    /**
     * 获取选择的数据
     * @return
     */
    public List<T> getSelect() {
        return mListAdapter.getSelect();
    }


    /**
     * 刷新列表数据
     */
    public void notifyDataChange() {
        notifyDataChange(false);
    }

    /**
     * 刷新列表数据
     * @param callSelectChange true:调用{@link  OnSelectChangeListener}回调接口
     */
    public void notifyDataChange(boolean callSelectChange) {
        if(mSelectionList==null)return;
        if(data.isEmpty()){
            mSelectionList.setVisibility(View.GONE);
            mEmptyView.show(emptyHint, null,null,null);
        }else{
            mSelectionList.setVisibility(View.VISIBLE);
            if(!mEmptyView.isHide()){
                mEmptyView.hide();
            }
        }
        mListAdapter.notifyDataChange(callSelectChange);
    }


}
